package com.xliu.cs.openprojects.videoplay.util;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class FileUtils {

    private static final Map<String, String> fileTypeMap = init();

    private static Map<String, String> init() {
        HashMap<String, String> map = new HashMap<>();
        // video type
        map.put("avi", "video");
        map.put("mp4", "video");
        map.put("wmv", "video");
        map.put("rmvb", "video");
        map.put("mkv", "video");

        // image type
        map.put("jpg", "image");
        map.put("jpeg", "video");
        map.put("png", "video");

        return map;
    }

    public static String getFileType(File file) {
        if (file.isDirectory()) {
            return "dir";
        }
        String fileName = file.getName();
        int dotIndex = fileName.lastIndexOf('.');
        if (dotIndex < 0) {
            return "unknown";
        }
        String suffix = fileName.substring(dotIndex + 1).toLowerCase();

        return fileTypeMap.getOrDefault(suffix, "unknown");

    }


}
